<?php


namespace app\admin\controller;
use app\common\lib;
use app\common\lib\Auth;
use think\Db;
use think\Request;

class Person extends Base
{
    public function change_password()
    {
        if(request()->isAjax()) {
            $data = input('post.');
//            return show('200','ok',$data);
            $user_msg = $this->isLogin();
            /******************************两次密码不一致********************************************/
            $is_pwd = lib\Password::isPassword($data['password'],$data['new_password']);
            if(!$is_pwd) {
                $_login_msg = lib\Tools::get_login_msg($user_msg['id'],'2','修改密码失败-两次密码不一致');
                Model('UserLoginMsg')->save($_login_msg);
                return show('200','两次密码请保持一致！','0');
            }
            /******************************与原密码不一致********************************************/
            $is_old = Model('User')->get(['password'=>lib\Password::setPassword($data['old_password'])]);
            if(!$is_old) {
                $_login_msg = lib\Tools::get_login_msg($user_msg['id'],'2','修改密码失败-与原密码不一致');
                Model('UserLoginMsg')->save($_login_msg);
                return show('200','与愿密码请保持一致！','0');
            }
            $_login_msg = lib\Tools::get_login_msg($user_msg['id'],'1','修改密码成功');
            Model('UserLoginMsg')->save($_login_msg);
            Model('User')->save(['password'=>lib\Password::setPassword($data['new_password'])],['id'=>$user_msg['id']]);
            return show('200','修改密码成功！退出后生效！','1');
        }else{

            /*************************检测权限 开始***********************/
            $auth = new Auth();
            $request = Request::instance();
            if(!$auth->check($request->path(),$this->isLogin()->id)) {
                return $this->redirect('not_rule/index/index');
            }
            /*************************检测权限 结束***********************/
            return $this->fetch();
        }
    }

    public function about_person()
    {
        if(request()->isAjax()) {
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $user_msg = $this->isLogin();
            $map = array();
            $map['uid'] = $user_msg['id'];
            //关键字查询
            if(isset($status) && $status!="")
            {
                $map['status'] = $status;
            }
            try {
                $count = Db::name('user_login_msg')->where($map)->count();  //总条目数据
                $lists = Db::name('user_login_msg')->page($NowPage,$limits)->where($map)->select();  //当前页
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 200, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        /*************************检测权限 开始***********************/
        $auth = new Auth();
        $request = Request::instance();
        if(!$auth->check($request->path(),$this->isLogin()->id)) {
            return $this->redirect('not_rule/index/index');
        }
        /*************************检测权限 结束***********************/
        return $this->fetch();
    }
}